<script setup>

import {reactive, ref} from "vue";
import {useRouter} from "vue-router";
import Axios from "axios";

document.title = "23-AJAX系统用户登录案例";
const api = "http://1.116.198.235/article/api/zxnews.php?type=login";
const localDate = reactive({
  user: "",
  password: "",
});

const agree = ref(false);

const router = useRouter();

async function login() {
  if (localDate.user === "") {
    alert("提示：账号不能为空！");
  } else if (localDate.password === "") {
    alert("提示：密码不能为空！");
  } else if (!agree.value === true) {
    alert("提示：请勾选同意框！");
  } else {
    try {
      const res = await Axios.post(api, localDate);
      let result = res.data;
      console.log("登录成功的数据：", result); // 添加这行
      if (result.code === "ok") {
        localStorage.setItem("userLoginToken", result.result);
        alert("提示：登录成功！");

        await router.push("/list");

      } else if (result.code === "error1") {
        alert("提示：账号或密码错误！");
      } else if (result.code === "error2") {
        alert("提示：账号或密码不能为空！");
      } else {
        alert("提示：登录失败！");
      }
    } catch (error) {
      console.error("登录请求失败", error);
      alert("提示：登录失败！");
    }
  }
}

console.log(localStorage.getItem('userLoginToken'))
</script>


<template>
  <div class="background">

    <div class="form-box">
      <div class="title">
        <div class="avatar-box">
          <img class="prew" src="/public/images/4.jpg" alt="">
        </div>
        <span>SDWK</span>
        <p>WELCOME <br> TO LOGIN</p>
      </div>
      <form class="form-horizontal">
        <!-- 正确 -->
        <!-- has-error 错误的类 -->
        <div class="form-group has-success has-feedback">
          <label class="control-label" for="inputSuccess2">账号</label>
          <input v-model.lazy="localDate.user" type="text" class="form-control" id="inputSuccess2"
                 aria-describedby="inputSuccess2Status" placeholder="请输入您的账号">
          <span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
          <span id="inputSuccess2Status" class="sr-only"></span>
        </div>
        <div class="form-group has-success has-feedback">
          <label class="control-label" for="inputSuccess2">密码</label>
          <input v-model.lazy="localDate.password" maxlength="50" type="text" class="form-control" id="inputSuccess2"
                 aria-describedby="inputSuccess2Status" placeholder="  请输入您的密码">
          <span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
          <span id="inputSuccess2Status" class="sr-only"></span>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <div class="checkbox">
              <label>
                <input v-model="agree" type="checkbox"> 同意勾选框
                <router-link to="/register" style="text-decoration: none">没有账号，请先注册？</router-link>
              </label>
            </div>
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <button @click.prevent="$router.push({ path: '/register' })" class="btn btn-default">注册</button>
            <button @click.prevent="login" class="btn btn-default">登录</button>
          </div>
        </div>
      </form>
    </div>
    <div style="height: 450px"></div>
  </div>


</template>


<style scoped>
.background {
  margin: 0;
  overflow: hidden;
  width: 100%;
  height: 100vh;
  background: linear-gradient(to bottom, #aae3a1, #a9e3a1, rgba(255, 255, 255));

}

.title {
  position: relative;
  float: left;
  width: 200px;
  height: 380px;
  margin: 10px 0;
  white-space: nowrap;
  overflow: hidden;
  text-align: center;
  /* text-overflow: ellipsis; */
  display: block;
  word-wrap: break-word;
  justify-items: center;
  border-right: 2px solid #a9e3a1;
}

.title span {
  position: absolute;
  top: 180px;
  left: 73px;
  font-size: 18px;
  font-weight: 700;
  color: #898888;
}

.title > p {
  position: absolute;
  top: 260px;
  left: 65px;
  text-align: center;
  font-family: "华文琥珀";
}

.avatar-box {
  position: absolute;
  width: 100px;
  height: 100px;
  top: 67px;
  left: 48px;
  overflow: hidden;
  border-radius: 50%;
  background-color: palegoldenrod;
}

.avatar-box img {
  width: 100%;
  height: 100%;
}

.form-box {
  width: 800px;
  height: 410px;
  margin: 100px auto;
  border: 1px solid #f2f2f2;
  border-radius: 10px;
  background-color: rgba(240, 240, 240, 0.4);

}

.form-group {
  margin: 46px 0;
}

.form-group > div > div > label > input:hover {
  cursor: pointer;
}

.form-group > div > button {
  width: 80px;
  height: 40px;
  border-radius: 20px;
  margin: 0 10px;
  background-color: #f8f4ed;
}

.form-horizontal {
  float: right;
  margin: -388px 7%;
}

.input-box {
  width: 500px;
}

.has-feedback {
  width: 500px;
}

</style>

